package com.sq.partner.manager.controller;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sq.partner.manager.common.Config;
import com.sq.partner.manager.common.Constants;
import com.sq.partner.manager.mybatis.model.GatewayUser;
import com.sq.partner.manager.mybatis.model.NocarEdrnoPower;
import com.sq.partner.manager.service.NocarEdrnoPowerService;
import com.sq.partner.manager.util.LogUtil;
import com.sq.partner.manager.util.Page;
import com.sq.partner.manager.util.SqlUtil;
import com.sq.partner.manager.util.StrUtil;

/**
 * 
 * @author 
 * 规则定义radProdInst
 */
@Controller
@RequestMapping("/edrnoConfiger")
public class NocarEdrnoPowerController {
	private static Logger logger = LogUtil.getLog(NocarEdrnoPowerController.class);
	@Resource
	NocarEdrnoPowerService nocarEdrnoPowerService;
	/**
	 * 跳转批改配置列表页面
	 * @param model 
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/list")
	public String productDefinition(ModelMap model,HttpServletRequest request) throws Exception {
		
		return "noCarProduct/edrno/list";
	}
	/**
	 * 跳转添加页面
	 * @param model
	 * @param request
	 * @param id
	 * @param flag  01
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/addEdrno")
	public String add(ModelMap model,HttpServletRequest request,String edrId,String flag) throws Exception {
		boolean channelFlag=true;
		if(StrUtil.isEmpty(edrId)){
			channelFlag=false;
		}else{
			NocarEdrnoPower edrnoPower = nocarEdrnoPowerService.queryNocarEdrnoPowerById(edrId);
			model.put("edrnoPower", edrnoPower);
		}
		if(StrUtil.isNotEmpty(flag)){
			model.put("copAdd", flag);
		}else{
			model.put("copAdd", "2");
		}
		model.put("channelFlag", channelFlag);
		
		return "noCarProduct/edrno/add";
	}
	
	@RequestMapping(value = "/saveEdrno")
	public @ResponseBody  boolean saveEdrno(NocarEdrnoPower nocarEdrnoPower,HttpServletRequest request) throws Exception {
		return nocarEdrnoPowerService.addNocarEdrnoPower(nocarEdrnoPower);
	}

	@RequestMapping(value = "/updateEdrno")
	public @ResponseBody  boolean updateEdrno(NocarEdrnoPower nocarEdrnoPower,HttpServletRequest request) throws Exception {
		return nocarEdrnoPowerService.updateNocarEdrnoPower(nocarEdrnoPower);
	}
	private String[] chooseEdrnos(String ids) {
		// TODO Auto-generated method stub
		if(StrUtil.isEmpty(ids)){
			return null;
		}
		String[] strs=ids.split(",");
		return strs;
	}

	/**
	 * 查询配置列表
	 * @param size
	 * @param current
	 * @param tradProdCode
	 * @throws Exception 
	 */
	@RequestMapping(value = "/queryEdrnoList")
	public @ResponseBody Page<Map<String,Object>> queryEdrnoList(Integer size,Integer current,String planCode,HttpSession session,String partnerCode) throws Exception{
		Page<Map<String,Object>> page=new Page<Map<String,Object>>();
		page.setCurrent(current);
		page.setSize(size);
		boolean flag=false;
		GatewayUser gatewayuser=(GatewayUser) session.getAttribute(Constants.SESSION_USER);
		String currentUser=gatewayuser.getUserCode();
		String usersConfig=Config.getProp("imple.nlife.userCodes");
		String[] codes=chooseEdrnos(usersConfig);
		if(codes!=null&&codes.length>0){
			flag=Arrays.asList(codes).contains(currentUser);
		}
		logger.info("查询批改配置列表");
		Map<String, Object> map = SqlUtil.buildPageMap(page);
		map.put("planCode", StrUtil.isEmpty(planCode)?null:planCode);
		map.put("partnerNo", StrUtil.isEmpty(partnerCode)?null:partnerCode);
		page.setMap(map);
		page.setObj(flag);
		return nocarEdrnoPowerService.queryNocarEdrnoPowers(page);
	}

	/**
	 * 删除
	 * @param tradProdDesc
	 * @param tradAppType
	 * @param prodInstCode
	 */
	@RequestMapping(value = "/deletEdrno")
	public @ResponseBody  boolean deletRule(String id,HttpSession session) throws Exception{
		GatewayUser user=(GatewayUser) session.getAttribute(Constants.SESSION_USER);
		logger.info("用户"+user.getUserCode()+"执行删除"+"批改ID为"+id);
		return nocarEdrnoPowerService.deletNocarEdrnoPower(id);
	} 
	
	/**
	 * 查询方案列表
	 * @param size
	 * @param current
	 * @param tradProdCode
	 * @throws Exception 
	 */
	@RequestMapping(value = "/queryNocarPlanList")
	public @ResponseBody Page<Map<String,Object>> queryNocarPlanList(Integer size,Integer current,String planCode,String planName,HttpSession session,String partnerNo) throws Exception{
		Page<Map<String,Object>> page=new Page<Map<String,Object>>();
		page.setCurrent(current);
		page.setSize(size);
		GatewayUser user=(GatewayUser) session.getAttribute(Constants.SESSION_USER);
		Map<String,Object> map=new HashMap<String, Object>();
		map.put("planCode", StrUtil.isEmpty(planCode)?null:planCode);
		map.put("planName", StrUtil.isEmpty(planName)?null:planName);
		map.put("partnerNo", StrUtil.isEmpty(partnerNo)?null:partnerNo);
		page.setMap(map);
		return nocarEdrnoPowerService.queryPlanPageByCriteria(page, user);
	}
}
